{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# _*Laurel or Yanny?*_ \n",
    "\n",
    "The latest version of this notebook is available on https://github.com/QISKit/qiskit-tutorial.\n",
    "\n",
    "For more information about how to use the IBM Q Experience (QX), consult the [tutorials](https://quantumexperience.ng.bluemix.net/qstage/#/tutorial?sectionId=c59b3710b928891a1420190148a72cce&pageIndex=0), or check out the [community](https://quantumexperience.ng.bluemix.net/qstage/#/community).\n",
    "\n",
    "***\n",
    "### Contributors\n",
    "Adam D. Perruzzi, IBM Q Consulting\n",
    "#### Copyright Attribution\n",
    "The audio files used in this notebook are equalized versions of the recording found at [https://www.vocabulary.com/dictionary/laurel](https://www.vocabulary.com/dictionary/laurel), used under Fair Use provisions and cited as follows: \"Text from Vocabulary.com, Copyright ©1998-2018 Thinkmap, Inc. All rights reserved.\"\n",
    "***\n",
    "#### Prerequisite\n",
    "To run this tutorial, you must have audio-related libraries installed. \n",
    "In MacOS, they can be installed by:\n",
    "```\n",
    "pip install pydub\n",
    "brew install portaudio\n",
    "pip install pyaudio\n",
    "```\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This program is a 'Hello World' type introduction to the world of quantum computing. It introduces some of the basic concepts of quantum computing in a way that is informative, fun, and engaging.\n",
    "\n",
    "One of the functions of quantum computers - more specifically of the qubits that comprise the quantum computer - is the ability to generate truly random numbers. Classical random number generators are often [pseudo-random](https://www.random.org/randomness), meaning that they are not truly random. Instead, these numbers are often generated using a seed, such as the curent timestamp.\n",
    "\n",
    "One can obtain a random number from a quantum computer by polling a single qubit. In doing so, the qubit 'chooses' to be either a 0 or a 1 in a purely random way. Repeating this polling many times and averaging the results will tend towards an average value of 0.5. That is the method we'll use for this program.\n",
    "\n",
    "And what can we use our random number for? Certainly a great number of important things, but also to try and settle one of the great debates of our time: Laurel or Yanny?\n",
    "\n",
    "In May 2018, an audio clip was discovered on Vocabulary.com accompanying the word [laurel](https://www.vocabulary.com/dictionary/laurel). The clip went viral as part of a tweet, and listeners from around the world weighed in on what they thought they heard. Some heard the intended word 'laurel' while others heard 'yanny'. There are some interesting notes on why this is the case in [this Wired article](https://www.wired.com/story/yanny-and-laurel-true-history).\n",
    "\n",
    "As the jury is still out on exactly what word people are hearing, let's ask a quantum computer what it thinks! The first step is to import the required libraries and initialize the quantum and classical registers that will be used for this program. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import ClassicalRegister, QuantumRegister\n",
    "from qiskit import QuantumCircuit, execute\n",
    "from qiskit.tools.visualization import plot_histogram\n",
    "from qiskit import IBMQ, available_backends, get_backend\n",
    "from pydub import AudioSegment\n",
    "from pydub.playback import play"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:01:26.202514Z",
     "start_time": "2018-10-09T01:01:25.114Z"
    }
   },
   "outputs": [],
   "source": [
    "#Set up the quantum and classical registers, and combine them into a circuit\n",
    "qr = QuantumRegister(1)\n",
    "cr = ClassicalRegister(1)\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "qc.h(qr[0]) #Create a superposition on the single quantum bit\n",
    "qc.measure(qr[0], cr[0]) #Measure the single bit, and store its value in the clasical bit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We also need to set up the credentials to access the IBM quantum computers. Make sure you've got an IBM Q Experience account set up, and you retrieve your API to paste below (or into the Qconfig.py file, if desired)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-09T01:01:46.845895Z",
     "start_time": "2018-10-09T01:01:38.676357Z"
    }
   },
   "outputs": [],
   "source": [
    "# Load saved IBMQ accounts\n",
    "IBMQ.load_accounts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now run the program on the quantum simulator. You can replace the selected simulation backend with the real quantum computer, if you don't mind a bit of a wait in the queue.\n",
    "\n",
    "You can also change the number of 'shots' taken, or the number of times the program is run. The smaller this number is, the more likely the average will favor one outcome over the other. The larger this number is, the more likely the results will be split 50-50. You'll be able to see the effects of this difference in the eventual audio output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:04.485868Z",
     "start_time": "2018-09-25T16:37:57.115893Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEZCAYAAACNebLAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAG/ZJREFUeJzt3X2YVnW97/H3p0EyH46PYykDMjQjO0iPbQfUY5maBPQwtHsw9CpTK/beSXbs6njolByjJ0t37dyQOyy3aNtG0rbMSQTPTtlHtxkMaelgwAQUg2g8ZVomAd/zx1qDNzdrmLlhZq2bmc/ruuZird/9u9f95WJmPqyH3++niMDMzKzcq4ouwMzMqpMDwszMMjkgzMwskwPCzMwyOSDMzCyTA8LMzDI5IMzMLJMDwszMMjkgzMws05CiCzgQxx9/fIwcObLoMszMDirLly/fHBG1PfU7qANi5MiRtLW1FV2GmdlBRdJvetPPl5jMzCxTbgEhaZKklZI6JM3IeP2bkp5Iv1ZJ+n1etZmZ2d5yucQkqQaYA0wAOoFlklojYkVXn4i4uqT/J4E35VGbmZlly+sMYjzQERFrImI70AJM2Uf/i4Ef5FKZmZllyisghgHrS/Y707a9SDoZq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0im0NgKnAj8kmRZ0U8B3ya5QbYjIi4pqFSzQUXSl4C/Ar4YEb8oup5q4zOIfLxIctnoIuBHwBeBepIZXJ8k+Xf4Vvrnl7s5hpn1vS8BTwEPS/rv6bT7lJztD2o+g8hZ+mjrB4CrgL+QXPt8kGT96YcjYkuB5ZkNeJIuJFnn/XmSdVeeJvmZ/BRwY0TcVGB5VcUBUaB0FtdpwN+QnGVcFRE/LbIms4EsXXvlX0luRK8imUrjzSRrT0PyJOEDwN9GxKCf6sYBUQXSR+nGASsj4vdF12M2UKUT8x0XEZslDQf+FBFbJA0DtpGsP30TsDQiZhdZazVwQJiZsccg1iuB/xURw4quqWi+SW1mxh4zLLcDXyuylmrhMwgzM8vkMwgzM8vkgDAzs0wOCDMzy+SAMDOzTA4IMzPL5IAwM7NM/x/Lc0/Cwj269wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "backend = 'ibmq_qasm_simulator' #Replace 'ibmq_qasm_simulator' with 'ibmqx5' to run on the quantum computer\n",
    "shots_sim = 100 #Adjust this number as desired, with effects as described above\n",
    "\n",
    "job_sim = execute(qc, backend, shots=shots_sim) #Run job on chosen backend for chosen number of shots\n",
    "stats_sim = job_sim.result().get_counts() #Retrieve results\n",
    "\n",
    "#Select '0' to represent 'laurel'\n",
    "if '0' not in stats_sim.keys():\n",
    "    stats_sim['laurel'] = 0\n",
    "else:\n",
    "    stats_sim['laurel'] = stats_sim.pop('0')\n",
    "\n",
    "#Which leaves '1' to represent 'yanny'\n",
    "if '1' not in stats_sim.keys():\n",
    "    stats_sim['yanny'] = 0\n",
    "else:\n",
    "    stats_sim['yanny'] = stats_sim.pop('1')\n",
    "\n",
    "plot_histogram(stats_sim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This graph shows how often the qubit was measured to be a 0, and how often it was measured to be a 1. We can use these results to help us find the answer to the laurel/yanny debate.\n",
    "\n",
    "We can do this by starting with two separate audio tracks, one for laurel and one for yanny. These tracks have been equalized to highlight one name or the other. First we have to install the required libraries. If you're having trouble, follow the guide for pydub installation available [here](https://github.com/jiaaro/pydub#installation)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:04.493584Z",
     "start_time": "2018-09-25T16:38:04.487610Z"
    }
   },
   "outputs": [],
   "source": [
    "#Import two tracks\n",
    "laurel = AudioSegment.from_wav('laurel_or_yanny_audio_files/laurel.wav')\n",
    "yanny = AudioSegment.from_wav('laurel_or_yanny_audio_files/yanny.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you've never been able to hear one name or the other, take a listen to either or both of the two tracks below. I can't guarantee you'll hear both distinctly, but these tracks will get you as close as possible!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:06.634455Z",
     "start_time": "2018-09-25T16:38:04.495351Z"
    }
   },
   "outputs": [],
   "source": [
    "play(laurel) #Listen to the laurel-specific track"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:08.444159Z",
     "start_time": "2018-09-25T16:38:06.636836Z"
    }
   },
   "outputs": [],
   "source": [
    "play(yanny) #Listen to the yanny-specific track"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can apply our results from the quantum computer to these audio tracks. We can do this by proportionally reducing the volume for the less common track, and increasing the volume for the more common track."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:10.260163Z",
     "start_time": "2018-09-25T16:38:08.446178Z"
    }
   },
   "outputs": [],
   "source": [
    "#Modify the volumes based on the results of the experiment\n",
    "laurel = laurel + ((100*stats_sim['laurel']/shots_sim)-50) #Laurel\n",
    "yanny = yanny + ((100*stats_sim['yanny']/shots_sim)-50) #Yanny\n",
    "\n",
    "#Mix the two together and play the result\n",
    "mixed = laurel.overlay(yanny)\n",
    "play(mixed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you're having trouble getting the above clip to play in the notebook, you can run the code below to download the clip using your audio player of choice. The audio file will be placed in the same folder as the other audio files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:10.268848Z",
     "start_time": "2018-09-25T16:38:10.262550Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<_io.BufferedRandom name='laurel_or_yanny_audio_files/quantumLaurelYanny.wav'>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mixed.export('laurel_or_yanny_audio_files/quantumLaurelYanny.wav', format='wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the results above are roughly 50-50, the combination will sound similar to the original track. If the experiment was conducted with fewer shots, the clip may lean more towards one sound or another. Go back earlier in the program to change the number of shots, re-run, and see if you can get one or the other to sound stronger or weaker.\n",
    "\n",
    "You can also change the backend to run this on an actual quantum computer, instead of a simulation. It may take a while to run, as there may be a queue for the machine.\n",
    "\n",
    "***\n",
    "\n",
    "And there you have it! Clearly the quantum computer can't make up its mind what it hears either, so the question will just have to go unanswered for the time being :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-25T16:38:12.351526Z",
     "start_time": "2018-09-25T16:38:10.270904Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installed packages are as the following\n",
      "Python 3.7.0\n",
      "\n",
      "# packages in environment at /anaconda3:\n",
      "#\n",
      "# Name                    Version                   Build  Channel\n",
      "IBMQuantumExperience      2.0.2                     <pip>\n",
      "numpy                     1.15.1           py37h6a91979_0  \n",
      "numpy-base                1.15.1           py37h8a80b8c_0  \n",
      "numpydoc                  0.8.0                    py37_0  \n",
      "qiskit--QISKIT-VERSION                              <pip>\n",
      "scipy                     1.1.0            py37h28f7352_1  \n"
     ]
    }
   ],
   "source": [
    "print(\"Installed packages are as the following\")\n",
    "!python --version\n",
    "print()\n",
    "!conda list 'qiskit|IBMQuantumExperience|numpy|scipy'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "48px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
